package com.gxa.zhonglong.controller;

import com.alibaba.excel.EasyExcel;
import com.gxa.zhonglong.pojo.StoreBarter;
import com.gxa.zhonglong.pojo.WarehouseWarrant;
import com.gxa.zhonglong.pojo.Where;
import com.gxa.zhonglong.service.FixedCodingService;
import com.gxa.zhonglong.service.InventoryService;
import com.gxa.zhonglong.service.StoreBarterService;
import com.gxa.zhonglong.service.StoreReturnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/28 19:30
 * Description:
 * Version: V1.0
 */
@Api(tags = "所有导出接口api")
@CrossOrigin
@RestController
@RequestMapping("/export")
public class ExportController {

    @Autowired
    StoreBarterService storeBarterService=null;
    @Autowired
    StoreReturnService storeReturnService=null;
    @Autowired
    InventoryService inventoryService=null;
    @Autowired
    FixedCodingService fixedCodingService=null;

    @GetMapping("/commodityWarehousing")
    @ApiOperation("商品入库导出接口")
    public void commodityWarehousing(Where where,HttpServletResponse response ) throws IOException {
        System.out.println(where);
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode("商品入库", "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), WarehouseWarrant.class)
                .sheet("商品入库").doWrite(inventoryService.selectByWhereNoPage(where));
    }

    @GetMapping("/fixedCode")
    @ApiOperation("固定编码入库导出接口")
    public void fixedCode(Where where, HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode("固定编码入库", "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), WarehouseWarrant.class)
                .sheet("编码入库").doWrite(fixedCodingService.selectByWhereNoPage(where));
    }

    @GetMapping("/storesAllocating")
    @ApiOperation("门店调拨导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "创单日期from(非必须)",name = "from"),
            @ApiImplicitParam(value = "创单日期to(非必须)",name = "to"),
            @ApiImplicitParam(value = "商品类型(非必须)",name = "storeBarterGclass"),
            @ApiImplicitParam(value = "入库门店(非必须)",name = "storeBarterNews"),
            @ApiImplicitParam(value = "入库柜台(非必须)",name = "storeBarterNewc"),
            @ApiImplicitParam(value = "出库门店(非必须)",name = "storeBarterOlds"),
            @ApiImplicitParam(value = "出库柜台(非必须)",name = "storeBarterOldc"),
            @ApiImplicitParam(value = "创单人(非必须)",name = "storeBarterCusername")
    })
    public void storesAllocating(HttpServletRequest request, HttpServletResponse response, Timestamp from, Timestamp to,
                                 String storeBarterGclass, String storeBarterNews,
                                 String storeBarterNewc, String storeBarterOlds,
                                 String storeBarterOldc, String storeBarterCusername
    ) throws IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("count")==null){
            session.setAttribute("count",1);
            System.out.println("第"+session.getAttribute("count")+"次导出请求进来");
        }else{
            session.setAttribute("count",(Integer)session.getAttribute("count")+1);
            System.out.println("第"+session.getAttribute("count")+"次导出请求进来");
        }
        List<StoreBarter> list=null;
        if(session.getAttribute("expstore")==null){
            System.out.println("list是null");
            list=storeBarterService.selectByField2(from, to, storeBarterGclass,
                    storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername);
            session.setAttribute("expstore",list);
            System.out.println("expstore:"+session.getAttribute("expstore"));
        }else {
            System.out.println("list不是null");
            System.out.println("expstore:"+session.getAttribute("expstore"));
            list=( List<StoreBarter>)session.getAttribute("expstore");
            session.removeAttribute("expstore");
            System.out.println("移除过后");
            System.out.println("expstore:"+session.getAttribute("expstore"));
        }
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode("门店调拨", "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), StoreBarter.class)
                .sheet("门店调拨").doWrite(list);
    }

    @GetMapping("/storeReturn")
    @ApiOperation("门店退货导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "创单日期from(非必须)",name = "from"),
            @ApiImplicitParam(value = "创单日期to(非必须)",name = "to"),
            @ApiImplicitParam(value = "商品类型(非必须)",name = "storeBarterGclass"),
            @ApiImplicitParam(value = "入库门店(非必须)",name = "storeBarterNews"),
            @ApiImplicitParam(value = "入库柜台(非必须)",name = "storeBarterNewc"),
            @ApiImplicitParam(value = "出库门店(非必须)",name = "storeBarterOlds"),
            @ApiImplicitParam(value = "出库柜台(非必须)",name = "storeBarterOldc"),
            @ApiImplicitParam(value = "创单人(非必须)",name = "storeBarterCusername")
    })
    public void toreReturn(HttpServletResponse response,Timestamp from, Timestamp to,
                           String storeBarterGclass, String storeBarterNews,
                           String storeBarterNewc, String storeBarterOlds,
                           String storeBarterOldc, String storeBarterCusername
    ) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode("门店退货", "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), StoreBarter.class)
                .sheet("门店退货").doWrite(storeReturnService.selectByField2(from, to, storeBarterGclass,
                storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername));
    }
}